# -*- coding: cp1251 -*-

# LIBS
import pygame
import sys

# BASE
import btasks


# CONSTS

# PYGAME INTERNAL CODES
MOUSE_BUTTON_LEFT       = 1
MOUSE_BUTTON_MIDDLE     = 2
MOUSE_BUTTON_RIGHT      = 3
MOUSE_WHEEL_UP          = 4
MOUSE_WHEEL_DOWN        = 5
KEY_UP                  = 273
KEY_DOWN                = 274
KEY_LEFT                = 276
KEY_RIGHT               = 275
KEY_ESC                 = 27

# VIRTUAL KEYS

class VirtualKey :
    def __init__ ( self ) :
        self.push       = btasks.Event ()
        self.pop        = btasks.Event ()


key_click               = VirtualKey ()
key_up                  = VirtualKey ()
key_down                = VirtualKey ()
key_left                = VirtualKey ()
key_right               = VirtualKey ()
key_back                = VirtualKey ()

# HARDWARE MAPPING

mouse_mapping           = \
{   MOUSE_BUTTON_LEFT   : key_click
}

keyboard_mapping        = \
{   KEY_LEFT            : key_left
,   KEY_RIGHT           : key_right
,   KEY_UP              : key_up
,   KEY_DOWN            : key_down
,   KEY_ESC             : key_back
}

# HANDLER

def handle () :

    for event in pygame.event.get () :

        if event.type == pygame.QUIT :
            return False

        if event.type == pygame.KEYUP :
            if event.key in keyboard_mapping :
                keyboard_mapping [ event.key ].pop ()

        if event.type == pygame.KEYDOWN :
            if event.key in keyboard_mapping :
                keyboard_mapping [ event.key ].push ()

        if event.type == pygame.MOUSEBUTTONUP :
            if event.button in mouse_mapping :
                mouse_mapping [ event.button ].pop ()

        if event.type == pygame.MOUSEBUTTONDOWN :
            if event.button in mouse_mapping :
                mouse_mapping [ event.button ].push ()

    return True
